/* linkit.js
 * By François Leporcq
 * MIT Licensed.
 */
var SVGImage  = AbstractImage.extend({
    _className: "SVGImage",
    init: function(origin, src, width, height){
        var height = (height != null) ? height : width;
        if(width == null){
            var size = this._getImageSize(src);
            var width = size.width;
            var height = size.height;
        }

        this._super(origin, src, width, height);
    },

    _getSVG: function(src){
        var xhr = new XMLHttpRequest();
        xhr.open("GET", src, false);
        try{
            xhr.send();
        }
        catch (error){
            if(error instanceof XMLHttpRequestException){
                if(error.code == 101){
                    throw new Error("If you want to take advantage of image/SVG auto sizing, you must use an svg image accesible on the same HTTP server where the linkit.js library is hosted");
                }
            }else{
                throw error;
            }
        }
        return xhr.responseXML;
    },
    _getImageSize: function(src){
        var xml = this._getSVG(src);
        var svg  = xml.getElementsByTagName("svg")[0];
        return {
            "width":parseInt(svg.getAttribute("width")),
            "height":parseInt(svg.getAttribute("height"))
        }
    }
});